package com.itheima.alipay.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.itheima.alipay.config.AlipayConfigInfo;
import com.itheima.alipay.domain.AlipayBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Xiao
 * @version 1.0
 * @description
 * @date 2021/11/16
 */
@RestController
@RequestMapping("/order")
public class AlipayController {
    /**
     * 快捷支付调用支付宝支付接口
     * @param alipayBean
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/alipay")
    public void alipayIumpSum(AlipayBean alipayBean, HttpServletResponse response)
            throws Exception {
        // 获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfigInfo.gatewayUrl, AlipayConfigInfo.app_id,
                AlipayConfigInfo.merchant_private_key, "json", AlipayConfigInfo.charset,
                AlipayConfigInfo.alipay_public_key, AlipayConfigInfo.sign_type);
        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfigInfo.return_url);
        alipayRequest.setNotifyUrl(AlipayConfigInfo.notify_url);
        alipayBean.setOut_trade_no(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + alipayBean.getOut_trade_no()
                + "\"," + "\"total_amount\":\"" + alipayBean.getTotal_amount()
                + "\"," + "\"subject\":\"" + alipayBean.getSubject()
                + "\"," + "\"body\":\"" + alipayBean.getBody() + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        // 请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println(result);
        AlipayConfigInfo.logResult(result);// 记录支付日志
        response.setContentType("text/html; charset=" + AlipayConfigInfo.charset);
        PrintWriter writer = response.getWriter();
        writer.write(result);
        writer.flush();
        writer.close();
    }

    /**
     * 支付完成回调验证操作
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("/notify_url")
    public void Notify(HttpServletResponse response, HttpServletRequest request) throws Exception {
        System.out.println("----------------------------notify_url------------------------");
        // 商户订单号
        String out_trade_no = new String(
                request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 付款金额
        String total_amount = new String(
                request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
        // 支付宝交易号
        String trade_no = new String(
                request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 交易说明
        String cus = new String(
                request.getParameter("body").getBytes("ISO-8859-1"), "UTF-8");
        // 交易状态
        String trade_status = new String(
                request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
        if (trade_status.equals("TRADE_SUCCESS")) {
            System.err.println("交易成功！");
        } else {
            System.err.println("交易失败！");
        }
    }

    /**
     * 同步通知的页面的Controller
     * @return
     */
    @GetMapping("/return_url")
    public String Return_url()  {
        System.err.println("TRADE_SUCCESS！");
        return "TRADE_SUCCESS！";
    }

}
